<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryOrderRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="type">
        <el-input
            v-model="queryParams.wareoutNo"
            placeholder="请输入订单编号"
            clearable
        />
      </el-form-item>
      <el-form-item label="审批状态" prop="approvalStatus" style="width: 220px">
        <el-select v-model="queryParams.status" placeholder="请选择审批状态" clearable>
          <el-option v-for="dict in approval_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleOrderQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetOrderQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['customer:outbound:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getOutboundList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="outboundList">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="订单编号" align="center" prop="wareoutNo" />
      <el-table-column label="审批状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="approval_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="完成状态" align="center" prop="approvalStatus" >
        <template #default="scope">
          <dict-tag :options="wareout_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row.id)" v-hasPermi="['customer:outbound:edit']" v-if="scope.row.status==='0'" >修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['customer:outbound:remove']" v-if="scope.row.status==='0'">删除</el-button>
          <el-button link type="primary" icon="Document" @click="handleQuery(scope.row.id)" v-hasPermi="['customer:outbound:query']" v-if="scope.row.status==='1'">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

   <!--详情页面-->
    <el-dialog  v-model="centerDialogVisible" title="入库清单" width="900" center>
      <span style="font-size: 20px">出库商品清单:</span>
      <el-table v-loading="loading" :data="orderData.orderNumber">
        <el-table-column label="商品名称" align="center" prop="name">
          <template #default="scope">
            <el-tag >{{getGood(scope.row.goodsId).name}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="商品规格" align="center" prop="specs" >
          <template #default="scope">
            <el-tag >{{getGood(scope.row.goodsId).specs}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="商品类别" align="center" prop="type">
          <template #default="scope">
            <dict-tag :options="goods_type" :value="getGood(scope.row.goodsId).type"/>
          </template>
        </el-table-column>
        <el-table-column label="出库商品数量" align="center" prop="outboundNumber"/>
      </el-table>
    </el-dialog>

     <!--修改出库清单对话框-->
    <el-dialog title="修改入库清单页面" v-model="openUpdate" width="900px" append-to-body>
        <el-table v-loading="loading" :data="orderData.orderNumber">
          <el-table-column label="商品名称" align="center" prop="name">
            <template #default="scope">
              <el-tag >{{getGood(scope.row.goodsId).name}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="商品规格" align="center" prop="specs" >
            <template #default="scope">
              <el-tag >{{getGood(scope.row.goodsId).specs}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="商品类别" align="center" prop="type">
            <template #default="scope">
              <dict-tag :options="goods_type" :value="getGood(scope.row.goodsId).type"/>
            </template>
          </el-table-column>
          <el-table-column label="存储商品数量" align="center" prop="number" />
          <el-table-column label="出库商品数量" align="center" prop="outboundNumber">
            <template #default="scope">
              <el-input-number v-model="scope.row.outboundNumber" :min="0" :max="scope.row.number"  />
            </template>
          </el-table-column>
        </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitUpdate">确 定 修 改</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 添加出库对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form :model="queryParams" ref="queryOutbound" :inline="true" label-width="68px">
        <el-form-item label="入库名称" prop="name">
          <el-input
              v-model="queryParams.name"
              placeholder="请输入入库名称"
              clearable
          />
        </el-form-item>
        <el-form-item label="仓库名称" prop="warehouseId">
          <el-select v-model="queryParams.warehouseId" style="width: 150px">
            <el-option
                v-for="item in warehouseList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="getList" >搜索</el-button>
          <el-button icon="Refresh" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="orderList" @row-click="handleRowClick" >
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="订单编号" align="center" prop="orderNo" />
        <el-table-column label="订单名称" align="center" prop="name" />
        <el-table-column label="仓库名称" align="center">
          <template #default="scope">
            {{getWarehouseId(scope.row.warehouseId).name}}
          </template>
        </el-table-column>
      </el-table>
      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-dialog>

    <!--出库商品选择页面-->
    <el-dialog :title="title" v-model="outboundGoods" width="1000px" append-to-body>
      <el-button @click="outboundGoods=false;open=true; title = '点击要出库订单所在的行，即可进行出库'" icon="ArrowLeft">返 回</el-button>
      <el-table v-loading="loading" :data="orderData.orderNumber">
          <el-table-column label="商品名称" align="center" prop="name">
            <template #default="scope">
              <el-tag >{{getGood(scope.row.goodsId).name}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="商品规格" align="center" prop="specs" >
            <template #default="scope">
              <el-tag>{{getGood(scope.row.goodsId).specs}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="商品类别" align="center" prop="type">
            <template #default="scope">
              <dict-tag :options="goods_type" :value="getGood(scope.row.goodsId).type" />
            </template>
          </el-table-column>
          <el-table-column label="存储商品数量" align="center" prop="number" />
          <el-table-column label="出库商品数量" align="center" >
            <template #default="scope">
              <el-input-number v-model="scope.row.outboundNumber" :min="1" :max="scope.row.number" label="出库商品数量"/>
            </template>
          </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitOutbound">确 定 提 交</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Outbound">
import {listGoods} from "@/api/customer/goods";
import {listWarehouse} from "@/api/warehouseManagement/warehouse";
import user from "@/store/modules/user";
import { getStore, listStore} from "@/api/customer/store.js";
import {addOutbound, getOutbound, listOutbound, updateOutbound,delOutbound} from "@/api/customer/outbound";

const { proxy } = getCurrentInstance();
const { goods_type,store_type,approval_status,shelf_prices,wareout_status } = proxy.useDict('goods_type',"store_type","approval_status","shelf_prices","wareout_status");


//存储商品列表
const goodsList = ref([]);
//入库清单列表
const orderList = ref([]);
//出库清单列表
const outboundList = ref([]);
const open = ref(false);
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const warehouseList=ref()
const centerDialogVisible=ref(false)
const openUpdate=ref(false)
const outboundGoods=ref(false)
const warehouseId=ref()
const showSearch = ref(true);
const orderData = ref({
  orderNumber:[]
});

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    status: null,
    warehouseId: null,
    cUsername:null,
    wareoutNo:null
  },
});

const { queryParams, form, rules } = toRefs(data);


/** 查询储存仓库价格列表 */
function getWarehouseList() {
  listWarehouse().then(response => {
    warehouseList.value=response.rows
  })
}
/** 查询入库清单列表 */
function getList() {
  queryParams.value.cId=user().id;
  queryParams.value.status=3
  listStore(queryParams.value).then(response => {
    orderList.value= response.rows;
    total.value = response.total;
    loading.value = false;
    getOutboundList()
  })
}

/** 查询入库清单列表 */
function getOutboundList() {
  queryParams.value.cUsername=user().name;
  listOutbound(queryParams.value).then(response => {
    outboundList.value= response.rows;
    total.value = response.total;
    loading.value = false;
  })
}


//根据仓库名称返回仓库对象
function getWarehouseId(id) {
  return warehouseList.value.find(item => item.id === id)
}

//根据商品编号返回商品
function getGood(id) {
  for (let i = 0; i < goodsList.value.length; i++) {
    if (goodsList.value[i].id === id) {
      return goodsList.value[i];
    }
  }
}


//查询订单详细信息
function orderQuery (id) {
  loading.value = true;
  queryParams.value.cId = user().id;
  delete queryParams.value.pageNum
  delete queryParams.value.pageSize
  //获取所有商品
  listGoods(queryParams.value).then(response => {
    goodsList.value = response.rows;
    //获取当前订单下的商品列表
    getStore(id).then(response => {
      orderData.value.orderNumber=[]
      for (let i = 0; i < response.data.orderNumber.length; i++) {
        orderData.value.orderNumber.push({
          goodsId: response.data.orderNumber[i].goodsId,
          number: response.data.orderNumber[i].number,
          wareoutOrederNo: response.data.orderNumber[i].orderNo,
          outboundNumber: 0,
          warehouseId:warehouseId.value,
        })
      }
      loading.value = false
    })
  });
}

//查询出库订单详细信息
function outboundQuery (id) {
  queryParams.value.cId = user().id;
  delete queryParams.value.pageNum
  delete queryParams.value.pageSize
  //获取所有商品
  listGoods(queryParams.value).then(response => {
    goodsList.value = response.rows;
    getOutbound(id).then(response => {
       orderData.value.orderNumber=response.data.outboundOrder
    })
  });
}

function handleQuery(id){
  outboundQuery(id)
  centerDialogVisible.value=true
}
//出库商品列表
function handleRowClick(row) {
  warehouseId.value=row.warehouseId
  orderData.value.orderNo=row.orderNo
  orderQuery(row.id)
  outboundGoods.value=true
  open.value=false
  title.value = "出库页面";
}

//重置
function reset() {
  proxy.resetForm("queryOutbound");
}

/** 订单搜索按钮操作 */
function handleOrderQuery() {
  queryParams.value.pageNum = 1;
  getOutboundList()
}

/** 新增按钮操作 */
function handleAdd() {
  title.value = "点击要出库订单所在的行，即可进行出库";
  open.value=true;
  getList()
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除储存入库清单编号为"' + row.id + '"的数据项？').then(function() {
    delOutbound({
      id: row.id,
      wareoutNo:row.wareoutNo
    }).then(() => {
      proxy.$modal.msgSuccess("删除成功");
      getOutboundList()
    })
  }).then(() => {
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});

}
/** 提交入库清单按钮操作 */
function submitOutbound(){
  open.value=false
  outboundGoods.value=false
  addOutbound({
    cUsername:user().name,
    outboundOrder:orderData.value.orderNumber
  }).then(response => {
    proxy.$modal.msgSuccess("出库成功");
    getOutboundList()
  });
}

/** 修改按钮操作 */
function handleUpdate(id) {
  openUpdate.value=true
  outboundQuery(id)
}

function submitUpdate(){
  updateOutbound(orderData.value.orderNumber).then(response => {
    proxy.$modal.msgSuccess("修改成功");
    openUpdate.value=false
    getOutboundList()
  });
}

function resetOrderQuery(){
  proxy.resetForm("queryOrderRef");
  queryParams.value.wareoutNo=null
  handleOrderQuery();
}
onMounted(()=>getWarehouseList())
getOutboundList()
</script>
